﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using log4net;
using RealState.BusinessObjects;
using RealState.BLL;

namespace RealState.W
{
    public partial class AddContent : System.Web.UI.Page
    {
        protected ILog log = LogManager.GetLogger("AddContent");
        private Contents contentBO;
        private ContentsBLL contentBLL;
        private ContentExtras contentExtraBO;
        private ContentExtrasBLL contentExtraBLL;
        private UserContents userContentBO;
        private UserContentsBLL userContentBLL;
        private CitiesBLL citiesBLL;
        private DistrictsBLL districtBLL;
        private TradeTypesBLL tradeTypeBLL;
        private ContentTypesBLL contentTypeBLL;
        private CategoriesBLL categoryBLL;
        private static int CategoryID = 0, UserID = 0, GroupID = 1;
        protected void Page_Load(object sender, EventArgs e)
        {
            //UserID=Convert.ToInt32(Session["CustomerID"]);
            UserID = 1;

            if (!Page.IsPostBack)
            {
                GetCities();
                GetTradeType();
                GetCategories();
                if (CategoryID != 0)
                {
                    
                    divFormInput.Visible = true;
                    divSelect.Visible = false;
                }
                else
                {
                   
                    divFormInput.Visible = false;
                    divSelect.Visible = true;

                }
            }
        }
        #region Loaddata
        private void GetCategories()
        {
            categoryBLL = new CategoriesBLL();
            try
            {
                drdCategories.DataSource = categoryBLL.GetCategoriess().Where(c => c.CategoryType == 2 && c.Deep == 2);
                drdCategories.DataTextField = "TITLE";
                drdCategories.DataValueField = "ID";
                drdCategories.DataBind();
                drdCategories.Items.Insert(0, new ListItem("-- Lựa chọn --", "0"));
            }
            catch (Exception ex)
            {
                log.Debug(ex.Message);
            }
        }

        private void GetTradeType()
        {
            tradeTypeBLL = new TradeTypesBLL();
            try
            {
                drdTradeType.DataSource = tradeTypeBLL.GetTradeTypess();
                drdTradeType.DataTextField = "TITLE";
                drdTradeType.DataValueField = "ID";
                drdTradeType.DataBind();
                drdTradeType.Items.Insert(0, new ListItem("-- Lựa chọn --", "0"));
            }
            catch (Exception ex)
            {
                log.Debug(ex.Message);
            }
        }
        private void GetContentType(int groupid)
        {
            contentTypeBLL = new ContentTypesBLL();
            try
            {
                drdContentType.DataSource = contentTypeBLL.GetContentTypess().Where(c => c.GroupId == groupid).ToList();
                drdContentType.DataTextField = "TITLE";
                drdContentType.DataValueField = "ID";
                drdContentType.DataBind();
                drdContentType.Items.Insert(0, new ListItem("-- Lựa chọn --", "0"));
            }
            catch (Exception ex)
            {
                log.Debug(ex.Message);
            }
        }
        private void GetCities()
        {
            citiesBLL = new CitiesBLL();
            try
            {
                drdCity.DataSource = citiesBLL.GetCitiess();
                drdCity.DataTextField = "CityName";
                drdCity.DataValueField = "ID";
                drdCity.DataBind();
                drdCity.Items.Insert(0, new ListItem("-- Lựa chọn --", "0"));
            }
            catch (Exception ex)
            {
                log.Debug(ex.Message);
            }
        }
        private void GetDistrict(int cityId)
        {
            districtBLL = new DistrictsBLL();
            try
            {
                drdDistrict.DataSource = districtBLL.GetDistrictss().Where(d => d.CityID == cityId).ToList();
                drdDistrict.DataTextField = "DistrictName";
                drdDistrict.DataValueField = "ID";
                drdDistrict.DataBind();
            }
            catch (Exception ex)
            {
                log.Debug(ex.Message);
            }
        }
        protected void drdCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetDistrict(Convert.ToInt32(drdCity.SelectedValue));
        }
        #endregion

        protected void Button2_Click(object sender, EventArgs e)
        {
            Session.Add("CID", drdCategories.SelectedValue);
            CategoryID =Convert.ToInt32(drdCategories.SelectedValue);
            lblCateName.Text = drdCategories.SelectedItem.Text;
            divFormInput.Visible = true;
            divSelect.Visible = false;

            divCompany.Visible = false;
            divCompany2.Visible = false;
            divProject.Visible = false;
            divPrice.Visible = false;
            divContentType.Visible = false;
            divAuction.Visible = false;
            divSaleType.Visible = false;
            if (CategoryID == 2) {
                divContentType.Visible = false;
                divAuction.Visible = false;
                divSaleType.Visible = false;
            }
            if (CategoryID == 3)
            {
                divContentType.Visible = false;
                divAuction.Visible = true;
                divSaleType.Visible = true;
                GroupID = 1;
            }
            if (CategoryID == 7 || CategoryID == 6) {
                divContentType.Visible = true;
                divAuction.Visible = true;
                divSaleType.Visible = true;
                GroupID = 2;
            }
            GetContentType(GroupID);
            if (CategoryID == 7 || CategoryID == 6 || CategoryID == 2 || CategoryID==3)
            {
                divCompany.Visible = true;
                divCompany2.Visible = true;
                divProject.Visible = false;
                divPrice.Visible = true;
            }
            if (CategoryID == 4)
            {
                divCompany.Visible = false;
                divCompany2.Visible = false;
                divProject.Visible = true;
                divPrice.Visible = false;
            }

        }
        public string getCategoryCode(int id) {
            categoryBLL = new CategoriesBLL();
            try
            {
                return categoryBLL.GetCategories(id).Code;
            }
            catch (Exception ex)
            {
                log.Debug(ex.Message);
            }
            return null;
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (!Page.IsValid)
                return;
            string imagePath = string.Empty;
            contentBO = new Contents();
            contentBLL = new ContentsBLL();
            contentExtraBO = new ContentExtras();
            contentExtraBLL = new ContentExtrasBLL();
            userContentBO = new UserContents();
            userContentBLL = new UserContentsBLL();     
            try
            {
                contentBO.Title = txtTitle.Text;
                contentBO.Content = txtContent.Text;
                contentBO.Duration = drdDuration.SelectedItem.Text;
                contentBO.DistrictId = Convert.ToInt32(drdDistrict.SelectedValue);
                contentBO.CityId = Convert.ToInt32(drdCity.SelectedValue);
                contentBO.CategoryId = CategoryID;
                contentBO.ContentTypeID = Convert.ToInt32(drdContentType.SelectedValue);
                contentBO.CreatedTime = DateTime.Now;
                contentBO.ImgPath = FileUploadImage.FileName;
                contentBO.IsActive = true;
                contentBO.IsRemove = false;
                contentBO.IsSpecial = false;
                contentBO.TradeTypeId = Convert.ToInt32(drdTradeType.SelectedValue);
                contentBO.UserID = UserID;
                contentBO.Code = "Code";
                //Insert
                int result = contentBLL.AddContents(contentBO);
                if (result > 0)
                {
                    if (FileUploadImage.FileName != null &&
                    FileUploadImage.FileName.Length > 0)
                    {
                        String currentDate = DateTime.Now.ToString("yyyyMMddhhmmssfff");
                        imagePath = "~/upload/" + currentDate +
                            GetImageExtension(FileUploadImage.FileName);

                        FileUploadImage.SaveAs(Server.MapPath(imagePath));
                    }

                    contentBO.Id = result;
                    contentBO.Code = getCategoryCode(CategoryID) + "-" + result;
                    //update code
                    contentBLL.UpdateContentsCode(contentBO);

                    contentExtraBO.ContentId = result;
                    contentExtraBO.CompanyName = txtCompanyAddress.Text;
                    contentExtraBO.CompanyAddress = txtCompanyAddress.Text;
                    contentExtraBO.Email = txtCompanyEmail.Text;
                    contentExtraBO.Fax = txtCompanyFax.Text;
                    contentExtraBO.Description = txtDescription.Text;
                    contentExtraBO.TaxCode = txtTaxCode.Text;
                    contentExtraBO.BusinessOwner = txtOwner.Text;
                    contentExtraBO.StartTime = Convert.ToDateTime(txtStartTime.Text);
                    contentExtraBO.Tel = txtCompanyTel.Text;

                    contentExtraBO.Status = drdStatus.SelectedItem.Text;
                    contentExtraBO.AnnualProfit = txtAnnualProfit.Text;
                    contentExtraBO.Assets = txtAsset.Text;
                    contentExtraBO.LastAnualProfit = txtLastAnnualProfit.Text;
                    contentExtraBO.LastRevenue = txtLastRevenue.Text;
                    contentExtraBO.Revenue = txtRevenue.Text;
                    contentExtraBO.SaleType = drdSaleType.SelectedItem.Text;
                    contentExtraBO.Reason = txtReason.Text;
                    contentExtraBO.Website = txtWebsite.Text;

                    contentExtraBO.ProjectTitle = txtProjectTitle.Text;
                    contentExtraBO.Location = txtLocaltion.Text;
                    contentExtraBO.Investor = txtInvestor.Text;
                    contentExtraBO.Note = txtNote.Text;

                    contentExtraBO.IsAuction = (ckbAuction.Checked) ? true : false;
                    contentExtraBO.Price =Convert.ToInt64( HiddenField1.Value);
                    contentExtraBO.TransType = (rdoTransType1.Checked) ? rdoTransType1.Text : rdoTransType2.Text;
                    //Insert
                    contentExtraBLL.AddContentExtras(contentExtraBO);

                    userContentBO.FullName = txtName.Text;
                    userContentBO.Address = txtAddressUser.Text;
                    userContentBO.ContentId = result;
                    userContentBO.Email = txtEmail.Text;
                    userContentBO.Mobile = txtMobile.Text;
                    userContentBO.Position = txtPosition.Text;
                    //Insert
                    userContentBLL.AddUserContents(userContentBO);
                }
            }
            catch (Exception ex)
            {
                log.Debug(ex.Message);
            }
        }


        private string GetImageExtension(string fileName)
        {
            return fileName.Substring(fileName.LastIndexOf('.'), fileName.Length - fileName.LastIndexOf('.'));
        }

    }
}
